#include <stdio.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

unsigned char edid[256] =
{
    0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x31,0xE7,0x00,0x00,0x00,0x00,0x00,0x00,
    0x08,0x20,0x01,0x03,0x80,0x59,0x32,0x78,0x0A,0xEE,0x91,0xA3,0x54,0x4C,0x99,0x26,
    0x0F,0x50,0x54,0x21,0x08,0x80,0x81,0xC0,0x81,0x00,0x81,0x40,0x81,0x80,0x95,0x00,
    0xA9,0xC0,0xA9,0x40,0x01,0x01,0x02,0x3A,0x80,0x18,0x71,0x38,0x2D,0x40,0x58,0x2C,
    0x45,0x00,0xC4,0x8E,0x21,0x00,0x00,0x1E,0x66,0x21,0x50,0xB0,0x51,0x00,0x1B,0x30,
    0x40,0x70,0x36,0x00,0xA0,0x5A,0x00,0x00,0x00,0x1E,0x00,0x00,0x00,0xFD,0x00,0x18,
    0x4B,0x1A,0x51,0x17,0x00,0x0A,0x20,0x20,0x20,0x20,0x20,0x20,0x00,0x00,0x00,0xFC,
    0x00,0x4C,0x6F,0x67,0x69,0x63,0x6F,0x6F,0x6C,0x0A,0x20,0x20,0x20,0x20,0x01,0x1B,
    0x02,0x03,0x2F,0xF1,0x48,0x90,0x1F,0x04,0x13,0x03,0x12,0x02,0x11,0x23,0x09,0x07,
    0x07,0x83,0x01,0x00,0x00,0x02,0x00,0x0F,0xE3,0x05,0x03,0x01,0x72,0x03,0x0C,0x00,
    0x40,0x00,0xB8,0x2D,0x20,0xD0,0x08,0x01,0x40,0x07,0x3F,0x40,0x50,0x90,0xA0,0xF3,
    0x39,0x80,0xD0,0x72,0x38,0x2D,0x40,0x10,0x2C,0x45,0x80,0xC4,0x8E,0x21,0x00,0x00,
    0x1E,0x01,0x1D,0x00,0xBC,0x52,0xD0,0x1E,0x20,0xB8,0x28,0x55,0x40,0xC4,0x8E,0x21,
    0x00,0x00,0x1E,0x0E,0x1F,0x00,0x80,0x51,0x00,0x1E,0x30,0x40,0x80,0x07,0x00,0x00,
    0x00,0x00,0x00,0x00,0x1C,0x66,0x21,0x56,0xAA,0x51,0x00,0x1E,0x30,0x46,0x8F,0x33,
    0x00,0x00,0x00,0x00,0x00,0x00,0x1E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x25
};

int write_to_file(const char * path_w, unsigned char* data, int size)
{
    int fd_w;

    ssize_t wr_size, rd_size;

    unsigned char *poffs;

    fd_w = open(path_w, O_WRONLY|O_CREAT|O_TRUNC,
                S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);

    if(fd_w == -1)
    {
        perror("Open For Wirte.");
        return -1;
    }

    rd_size = size;
    poffs = data;
    while(1)
    {
        wr_size = write(fd_w, poffs, rd_size); // 实际写入长度wr_size 只会小于或等于预设写入长度rd_size
        if(wr_size == -1)
        {
            perror("write to new file error\n");
            goto err;
        }

        if(wr_size == rd_size) // 当 实际写入长度 小于 预设写入长度，必须进行处理，以防丢包
            break;

        rd_size  -=  wr_size;

        poffs +=  wr_size;
    }

    close(fd_w);

    return 0;

err:
    return -1;
}


int main(int argc, char * argv[])
{
    write_to_file("1920x1080.bin", edid, sizeof(edid));
    return 0;
}
